<?php
header('content-type:text/html; charset=utf-8');
require_once('config.php');
//打开数据库连接
$conn = new mysqli($host,$user='root',$password='root',$database);
if($conn->connect_error){
    die("连接失败".$conn->connect_error);
}
$methods = $_POST['method'];
if($methods == 'show')
{
    //sql查询语句
    $sql="SELECT * FROM carinformation where caring = '0'";
    $arr=array();
    $res=$conn->query($sql);
    if($res->num_rows>0){
        while($row=$res->fetch_assoc()){
            array_push($arr,$row);
        }
        echo json_encode(array('code'=>200,'data'=>$arr));
    }
    else {
        echo json_encode(array('code'=>300,'data'=>0));
    }
}
if($methods == 'find')
{
    $id=$_POST['id'];
    //sql查询语句
    $sql="SELECT * FROM carinformation where id='$id'";
    $arr=array();
    $res=$conn->query($sql);
    if($res->num_rows>0){
        while($row=$res->fetch_assoc()){
            array_push($arr,$row);
        }
        echo json_encode(array('code'=>200,'data'=>$arr));
    }
    else {
        echo json_encode(array('code'=>300,'data'=>1));
    }
}
if($methods == 'note')
{
    $userid=$_POST['userid'];
    $carid=$_POST['carid'];
    $caruserlong=$_POST['caruserlong'];
    $carmoney=$_POST['carmoney'];
    $cardate=$_POST['cardate'];
    //sql查询语句
    $sql1="SELECT * FROM login where username='$userid'";
    $res=$conn->query($sql1);
    if($res->num_rows>0){
        $row=$res->fetch_assoc();
    }
    if($row['money']>$carmoney){
        $sql2="INSERT INTO carnote (carid , caruserlong , carmoney , cardate , userid , caring) values('$carid','$caruserlong','$carmoney','$cardate','$userid','1')";
        $conn->query($sql2);
        $money=$row['money']-$carmoney;
        $sql2="UPDATE login SET money = '$money' where username = '$userid'";
        $conn->query($sql2);
        $sql2="UPDATE carinformation SET caring = '1' where id = '$carid'";
        $conn->query($sql2);
        echo json_encode(array('code'=>200,'data'=>$money));
    }else{
        echo json_encode(array('code'=>300,'data'=>$row['money']));
    }
}
if($methods == 'dshow')
{
    $userid=$_POST['userid'];
    //sql查询语句
    $sql="SELECT * FROM carnote where userid = '$userid' and caring = '1'";
    $arr=array();
    $res=$conn->query($sql);
    if($res->num_rows>0){
        while($row=$res->fetch_assoc()){
            array_push($arr,$row);
        }
        echo json_encode(array('code'=>200,'data'=>$arr));
    }
    else {
        echo json_encode(array('code'=>300,'data'=>0));
    }
}
if($methods == 'dfind')
{
    $id=$_POST['id'];
    //sql查询语句
    $sql="SELECT * FROM carinformation where id=$id  ";
    $arr=array();
    $res=$conn->query($sql);
    if($res->num_rows>0){
        while($row=$res->fetch_assoc()){
            array_push($arr,$row);
        }
        echo json_encode(array('code'=>200,'data'=>$arr));
    }
    else {
        echo json_encode(array('code'=>300,'data'=>$id));
    }
}
if($methods == 'dover')
{
    $id=$_POST['id'];
    $userid=$_POST['userid'];
    //sql查询语句
    $sql="UPDATE carnote SET caring = '0' where carid = '$id' and userid = '$userid'";
    $arr=array();
    $res=$conn->query($sql);
    $sql="UPDATE carinformation SET caring = '0' , caralllong = caralllong - carlong where id = '$id'";
    $res=$conn->query($sql);
    echo json_encode(array('code'=>200,'data'=>1));
}
if($methods == 'hist')
{
    $userid=$_POST['userid'];
    //sql查询语句
    $sql="SELECT * FROM carnote where userid = '$userid' and caring = '0'";
    $arr=array();
    $res=$conn->query($sql);
    if($res->num_rows>0){
        while($row=$res->fetch_assoc()){
            array_push($arr,$row);
        }
        echo json_encode(array('code'=>200,'data'=>$arr));
    }
    else {
        echo json_encode(array('code'=>300,'data'=>0));
    }
}
if($methods == 'smoney')
{
    $userid=$_POST['userid'];
    //sql查询语句
    $sql="SELECT * FROM login where username = '$userid'";
    $arr=array();
    $res=$conn->query($sql);
    if($res->num_rows>0){
        $row=$res->fetch_assoc();
        $money=$row['money'];
        echo json_encode(array('code'=>200,'data'=>$money));
    }
    else {
        echo json_encode(array('code'=>300,'data'=>1));
    }
}
if($methods == 'sear')
{
    $carname = $_POST['carname'];
    if($carname!=""){
        $sql="SELECT * FROM carinformation where caring = '0' and carname = '$carname'";
    }else{
        $sql="SELECT * FROM carinformation where caring = '0'";
    }
    //sql查询语句
    $arr=array();
    $res=$conn->query($sql);
    if($res->num_rows>0){
        while($row=$res->fetch_assoc()){
            array_push($arr,$row);
        }
        echo json_encode(array('code'=>200,'data'=>$arr));
    }
    else {
        echo json_encode(array('code'=>300,'data'=>0));
    }
}
?>